package com.fastjrun.bms.dao.provider;

import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

public class CustomerBookSqlProvider {
  public String search(String name, String author, String isbn, Long pressId) {
    SQL sql =
        new SQL()
            .SELECT(
                "book.id,book.name,book.isbn,book.author,book.price,book.press_id as pressId,press.name as pressName")
            .FROM("book")
            .LEFT_OUTER_JOIN("press on book.press_id=press.id");
    if (StringUtils.hasText(name)) {
      sql.WHERE("book.name LIKE #{name}");
    }
    if (StringUtils.hasText(author)) {
      sql.WHERE("book.author LIKE #{author}");
    }
    if (StringUtils.hasText(isbn)) {
      sql.WHERE("book.isbn = #{isbn}");
    }
    if (pressId != null && pressId.longValue() > 0) {
      sql.WHERE("book.press_id = #{pressId}");
    }
    return sql.toString();
  }
}
